package repository

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"

	"github.com/go-redis/redis/v8"
	"github.com/spf13/viper"
)

// 初始化MariaDB连接
func InitDB() (*gorm.DB, error) {
	// 获取MariaDB配置
	dbUser := viper.GetString("database.user")
	dbPassword := viper.GetString("database.password")
	dbHost := viper.GetString("database.host")
	dbPort := viper.GetString("database.port")
	dbName := viper.GetString("database.name")

	dsn := dbUser + ":" + dbPassword + "@tcp(" + dbHost + ":" + dbPort + ")/" + dbName + "?charset=utf8mb4&parseTime=True&loc=Local"
	return gorm.Open(mysql.Open(dsn), &gorm.Config{})
}

// 初始化 Redis 连接
func InitRedis() (*redis.Client, error) {
	// 获取 Redis 配置
	redisHost := viper.GetString("redis.host")
	redisPort := viper.GetString("redis.port")
	redisPassword := viper.GetString("redis.password")

	return redis.NewClient(&redis.Options{
		Addr:     redisHost + ":" + redisPort,
		Password: redisPassword,
	}), nil
}
